package StudentProject.view;

import StudentProject.bean.Student;
import StudentProject.bean.User;
import StudentProject.dao.StudentDaoImp;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * 视图层，主要装界面
 */
public class View {

    private static Scanner input = new Scanner(System.in);

    //主页面
    public static User indexView() {
        System.out.println("------------欢迎来到学生管理系统------------");
        System.out.println("请输入账号:");
        String uname = input.next();
        System.out.println("请输入秘密:");
        String password = input.next();
        return new User(uname, password);
    }


    //管理员页面
    public static int managerMenuView() {
        while (true) {
            System.out.println("------------欢迎来到管理员系统------------");
            System.out.println("请根据提示输入:");
            System.out.println("0.退出");
            System.out.println("1.学生账号管理系统");
            System.out.println("2.学生信息管理系统");
            System.out.println("3.学生成绩管理系统");
            int item = input.nextInt();
            if (item < 0 || item > 3) {
                System.out.println("输入错误,重新输入");
                return managerMenuView();
            } else {
                return item;
            }
        }
    }

    //学生界面
    public static int studentService(){
        while (true) {
            System.out.println("------------欢迎来到学生系统------------");
            System.out.println("请根据提示输入:");
            System.out.println("0.退出");
            System.out.println("1.成绩查询");
            System.out.println("请输入：");
            int item = input.nextInt();
            if (item < 0 || item > 2) {
                System.out.println("输入错误,重新输入");
                return managerMenuView();
            } else {
                return item;
            }
        }
    }


    //学生账号信息管理界面
    public static int studentAccountMenuView(){
        while (true) {
            System.out.println("------------欢迎来到学生账号管理系统------------");
            System.out.println("请根据提示输入:");
            System.out.println("0.退出");
            System.out.println("1.添加学生账号");
            System.out.println("2.删除学生账号");
            System.out.println("3.修改学生密码");
            System.out.println("4.查询学生账号");
            System.out.println("请输入：");
            int item = input.nextInt();
            if (item < 0 || item > 4) {
                System.out.println("输入错误,重新输入");
                return managerMenuView();
            } else {
                return item;
            }
        }
    }

    /**
     * 添加学生账号视图
     *
     * @return 新的User对象User(uname, password)
     */
    public static User addMenuView() {
        System.out.println("------------学生账号添加------------");
        System.out.println("请输入账号:");
        String uname = input.next();
        System.out.println("请输入秘密:");
        String password = input.next();
        return new User(uname, password);
    }

    /**
     * 删除学生账号视图
     */
    public static String deleteMenuView() {
        System.out.println("------------学生账号删除------------");
        System.out.println("请输入要删除的学生账号:");
        String uname = input.next();
        return uname;
    }

    /**
     * 修改学生账号视图
     */
    public static User updateMenuView(){
        System.out.println("------------学生账号修改------------");
        System.out.println("请输入学生账号:");
        String uname = input.next();
        System.out.println("请输入要修改的学生密码:");
        String password = input.next();
        return new User(uname,password);
    }
    /**
     * 查询学生账号
     */
    public static String selectMenuView(){
        System.out.println("------------学生账号查询------------");
        System.out.println("请输入要查询的学生账号:");
        String uname = input.next();
        return uname;
    }
    //打印查询到的信息
    public static void printUserInfo(User user){
        System.out.println("用户id:" + user.getUserid());
        System.out.println("账号:" + user.getUname());
        System.out.println("密码:" + user.getPassword());
        if (user.getType() == 1){
            System.out.println("权限:管理员");
        }else {
            System.out.println("权限:学生");
        }
    }



    //学生信息系统 start
    //学生信息管理界面
    public static int studentInfoMenuView(){
        while (true) {
            System.out.println("------------欢迎来到学生信息管理系统------------");
            System.out.println("请根据提示输入:");
            System.out.println("0.退出");
            System.out.println("1.添加学生信息");
            System.out.println("2.删除学生信息");
            System.out.println("3.修改学生信息");
            System.out.println("4.查询学生信息");
            System.out.println("请输入：");
            int item = input.nextInt();
            if (item < 0 || item > 4) {
                System.out.println("输入错误,重新输入");
                return managerMenuView();
            } else {
                return item;
            }
        }
    }

    //学生信息添加界面
    public static Student addStudentInfoMenuView(){
        System.out.println("------------学生信息添加------------");
        System.out.println("输入添加学生姓名:");
        String name = input.next();
        System.out.println("输入添加学生年龄:");
        int age = input.nextInt();
        System.out.println("输入添加学生性别:");
        String  gender = input.next();
        return new Student(name,age,gender);
    }

    //

    //学生信息删除页面(主页面)
    public static Student deleteStudentMenuView(){
         boolean flage;
        StudentDaoImp studentDaoImp = new StudentDaoImp();
        while (true) {
            System.out.println("------------学生信息删除-----------");
            System.out.println("1.按照编号删除");
            System.out.println("2.按照姓名删除");
            System.out.println("选择删除需求:");
            int item = input.nextInt();
            switch (item){
                case 1:
                //删除方法1
                    int deleteStudentID = deleteStudentID();
                    flage = studentDaoImp.deleteId(deleteStudentID);
                    System.out.println(flage?"删除成功":"删除失败");
                    break;
                case 2:
                    String deleteStudentName = deleteStudentName();
                    flage = studentDaoImp.deleteName(deleteStudentName);
                    System.out.println(flage?"删除成功":"删除失败");
                    break;
                default:
                    System.out.println("输入错误，请重新输入");
            }
            return null;
        }
    }
    //学生信息删除方法1:按id进行删除
    public static int deleteStudentID(){
        System.out.println("请输入删除编号:");
        int student_id = input.nextInt();
        return student_id;
    }

    //学生信息删除方法2:按姓名进行删除
    public static String deleteStudentName(){
        System.out.println("请输入删除姓名:");
        String name = input.next();
        return name;
    }


    //修改学生信息
    public static Student updateStudentInfoMenuView(){
        System.out.println("------------学生信息修改------------");
        System.out.println("请输入修改的学生id:");
        int studentId = input.nextInt();
        System.out.println("请输入修改姓名:");
        String name = input.next();
        System.out.println("请输入修改年龄:");
        int age = input.nextInt();
        System.out.println("请输入修改性别:");
        String  gender = input.next();
        return new Student(studentId,name,age,gender);
    }

    //学生信息查询面(主页面)
    public static Student selectStudentMenuView(){
        StudentDaoImp studentDaoImp = new StudentDaoImp();

        while (true) {
            System.out.println("------------学生信息查询-----------");
            System.out.println("1.按照编号查询");
            System.out.println("2.按照姓名查询");
            System.out.println("3.查询所有学生信息");
            System.out.println("4.分页查询学生信息");
            System.out.println("5.排序查询学生信息");
            System.out.println("选择查询需求:");
            int item = input.nextInt();
            switch (item){
                case 1:
                    //查询方法1
                    int selectStudentID = View.selectStudentID();
                  Student student = studentDaoImp.selectId(selectStudentID);
                    View.printStudentInfo(student);
                    selectStudentMenuView();
                    break;
                case 2:
                    //查询2
                    String selectStudentName = selectStudentName();
                    Student student1 = studentDaoImp.selectAll(selectStudentName);
                    View.printStudentInfo(student1);
                    selectStudentMenuView();
                    break;
                case 3:
                    //查询3
                    studentDaoImp.select();
                    selectStudentMenuView();
                    break;
                case 4:
                    //查询4, 分页查询 + 人数与比例显示
                    int pageNumber = 1;
                    int pageSize = 5;
                    while (true) {
                        List<Student> students = studentDaoImp.selectStudents(pageNumber, pageSize);

                        System.out.println("以下为第 " + pageNumber + " 页学生信息：");
                        int maleCount = 0;
                        int femaleCount = 0;

                        for (Student student2 : students) {
                            System.out.println("学生id = " + student2.getStudent_id() + ", 学生姓名 = " + student2.getName() + ", 学生性别 = " + student2.getGender() + ", 学生年龄 = " + student2.getAge());
                            if (student2.getGender().equalsIgnoreCase("男")) {
                                maleCount++;
                            } else if (student2.getGender().equalsIgnoreCase("女")) {
                                femaleCount++;
                            }
                        }

                        int totalStudents = students.size();
                        double maleRatio = (double) maleCount / totalStudents * 100;
                        double femaleRatio = (double) femaleCount / totalStudents * 100;

                        System.out.println("男生数量：" + maleCount + "，占比：" + maleRatio + "%");
                        System.out.println("女生数量：" + femaleCount + "，占比：" + femaleRatio + "%");
                        System.out.println("总人数: " + totalStudents + "人");

                        System.out.println("按 1 继续查看下一页，按 2 退出：");
                        int choice = input.nextInt();

                        if (choice == 2) {
                            break;
                        } else if (choice == 1) {
                            pageNumber++;
                        }
                    }
                    selectStudentMenuView();
                    break;
                case 5:
                    //查询5 排序查询 按照id 姓名 年龄进行 升降排序
                    System.out.println("请选择排序方式：");
                    System.out.println("1. 按照 ID 排序");
                    System.out.println("2. 按照姓名排序");
                    System.out.println("3. 按照年龄排序");

                    item = input.nextInt();

                    System.out.println("请选择排序顺序：");
                    System.out.println("1. 升序");
                    System.out.println("2. 降序");

                    int orderItem = input.nextInt();

                    studentDaoImp.select(item, orderItem);
                    break;
                default:
                    System.out.println("输入错误，请重新输入");
                    selectStudentMenuView();
                    break;
            }
            return null;
        }
    }
    //学生信息查询方法1:按id进行查询
    private static int selectStudentID(){
        System.out.println("请输入查询编号:");
        int student_id = input.nextInt();
        return student_id;
    }

    //学生信息查询方法2:按姓名进行查询
    private static String selectStudentName(){
        System.out.println("请输入查询姓名:");
        String name = input.next();
        return name;
    }


    //打印查询的学生信息
    public static Student printStudentInfo(Student student){
        System.out.println("学生编号：" + student.getStudent_id());
        System.out.println("学生姓名:" + student.getName());
        System.out.println("学生年龄:" + student.getAge());
        System.out.println("学生性别:" + student.getGender());
        return student;
    }

    //学生信息系统 end

    //成绩管理系统start
    public static int studentGradeService(){
        while (true) {
            System.out.println("------------欢迎来到学生成绩管理系统------------");
            System.out.println("请根据提示输入:");
            System.out.println("0.退出");
            System.out.println("1.添加学生成绩");
            System.out.println("2.删除学生成绩");
            System.out.println("3.修改学生成绩");
            System.out.println("4.查询学生成绩");
            System.out.println("请输入：");
            int item = input.nextInt();
            if (item < 0 || item > 4) {
                System.out.println("输入错误,重新输入");
                return studentGradeService();
            } else {
                return item;
            }
        }
    }
}